#include <iostream>
#include <vector>
#include <map>

using namespace std;

// 使用map存下不符合条件的元素 [vaule => index]
// 每次遍历看是否有符合条件的情况，有则直接返回两个索引
vector<int> twoSum(vector<int>& nums, int target) {
    map<int, int> appera {};
    vector<int> result {};
    for (int i = 0, len = nums.size(); i < len; i++) {
        if (appera.count(target - nums[i]) > 0) {
            result.push_back(appera[target - nums[i]]);
            result.push_back(i);
            break;
        }

        appera[nums[i]] = i;
    }

    return result;
}

void printVector(vector<int>& nums) {
    for (int i = 0, len = nums.size(); i < len; i++) {
        cout << "nums[" << i << "] = " << nums[i] << "\n";
    }
}

int main() {
    vector<int> nums1 { 11, 2, 7, 15 };
    int target = 9;

    vector<int> result = twoSum(nums1, target);
    printVector(result);

    return 0;
}